<?php

namespace mpend\models;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * User
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class User extends \common\models\user\User {

    public function getDataCompleteness() {
        $attributes = [
            'nickname' => 0.4,
            'avatar' => 0.3,
            'meta.signature' => 0.1,
//            'gender' => 0.1,
//            'meta.cover' => 0.1,
//            'meta.birthday' => 0.1,
//            'meta.location' => 0.1,
        ];
        $progress = 0;
        foreach ($attributes as $attribute => $precent) {
            $value = ArrayHelper::getValue($this, $attribute);
            if ($attribute === 'avatar' && $this->getDefaultAvatar() == $value) {
                $value = null;
            }
            if (!empty($value)) {
                $progress += $precent;
            }
        }
        return $progress;
    }

    public function getWizard() {
        if (!$this->mobile) {
            return [
                'name' => 'bind',
                'text' => '绑定手机号后可以方便班组长与工人联系',
                'action' => [
                    'text' => '去绑定',
                    'openType' => 'getPhoneNumber'
                ],
            ];
        }
        $dataCompleteness = $this->getDataCompleteness();
        if ($dataCompleteness > 0.8) {
            return null;
        }
        $progress = Yii::$app->formatter->asPercent($dataCompleteness);
        return [
            'icon' => 'idcard',
            'name' => 'perfect',
            'text' => '资料完善度 ' . $progress,
            'action' => [
                'text' => '去完善'
            ],
        ];
        //完善个人资料后可以在记工簿中直接展示
    }

    public function getMemberType() {
        if ($this->member) {
            return [
                'text' => '认证会员',
                'color' => '#ff6600',
                'plain' => false,
            ];
        }
        if ($this->authented) {
            return [
                'text' => '认证成员',
                'color' => '#ff6600',
                'textColor' => '普通用户',
                'plain' => true,
            ];
        }
        return null;
    }

    /**
     * @inheritdoc
     */
    public function fields() {
        return [
            'id',
            'username',
            'nickname' => 'userName',
            'description',
            'avatar' => 'portrait',
            'gender' => function() {
                return $this->meta ? $this->meta->gender : 0;
            },
            'mobile' => function () {
                return $this->decryptMobile();
            },
            'actived',
            'authented',
            'memberType',
            'wizard',
        ];
    }

}
